package com.base.vistter.iframe.inverter.controller;

import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.utils.IoUtils;
import com.base.vistter.iframe.utils.UploadUtils;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.regex.Pattern;

@Controller
@Slf4j
public class FileController {

    public static final String IMAGE_REG = ".(xbm|tif|jfif|ico|tiff|gif|svg|jpeg|svgz|jpg|webp|png|bmp|pjp|apng|pjpeg|avif)$";
    public static final String VIDEO_REG = ".(ogm|wmv|mpg|webm|ogv|mov|asx|mpeg|mp4|m4v|avi)$";

    public static final String PDF_REG = ".pdf$";

    @GetMapping("/buffer")
    public void buffter(String filePath, HttpServletResponse response) throws IframeException {
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        if(Pattern.matches(IMAGE_REG,filePath)){
            response.setContentType("image/"+ UploadUtils.getFileExt(filePath)); // 设置返回的文件类型
        }else if(Pattern.matches(VIDEO_REG,filePath)){
            response.setContentType("video/"+ UploadUtils.getFileExt(filePath)); // 设置返回的文件类型
        }else if(Pattern.matches(PDF_REG, filePath)){
            response.setContentType("application/pdf;charset=UTF-8");
        }
        IoUtils.fileToStream(filePath , response);
    }

}
